package cl.tinet.trainee.salas.dao;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import cl.tinet.trainee.salas.model.ReservaSala;
import cl.tinet.trainee.salas.model.Segmentos;

/**
 * 
 * Implementacion DAO de la clase Reserva.
 * 
 * @author Naht-Sams
 */
public class ReservaDao implements ReservaDaoInterfaz {

    private EntityManager em;

    public ReservaDao(EntityManager em) {
        super();
        this.setEm(em);
    }

    /** {@inheritDoc} */
    public void agregarReservaSala(ReservaSala reservaSala) {
        em.persist(reservaSala);

    }

    /** {@inheritDoc} */
    public List<ReservaSala> buscarListaReservaSala(int codSala,
            String fechaSeleccionada) {
        Query q =
                em.createQuery("SELECT rs from ReservaSala rs "
                        + "where cod_sala_reserva=:codSala "
                        + "and fecha_inicio=:fechaInicio and estado=1");
        q.setParameter("codSala", codSala);
        q.setParameter("fechaInicio", fechaSeleccionada);
        // query.setParameter("login", usuario.getLogin());
        List<ReservaSala> listaReservas = q.getResultList();
        return listaReservas;
    }

    /** {@inheritDoc} */
    public void borrarReservaSala(ReservaSala reservaSala) {
        em.remove(reservaSala);
    }

    /** {@inheritDoc} */
    public List<ReservaSala> buscarReservaSalaAntesDeCrear(
            ReservaSala reservaSala) {
        SimpleDateFormat formatoHora2 = new SimpleDateFormat("HH:mm:ss");
        Query q =
                em.createNativeQuery("select cod_reserva from reserva_sala "
                        + "where cod_sala_reserva=:codSala "
                        + "and  ((:segmentoInicio BETWEEN (segmento_inicial) "
                        + "and SUBTIME(segmento_termino,'00:00:01')) "
                        + "or (:segmentoTermino BETWEEN ADDTIME(segmento_inicial,'00:00:01') "
                        + "and (segmento_termino))); ");
        q.setParameter("codSala", reservaSala.getCod_sala());
        q.setParameter("segmentoInicio",
                formatoHora2.format(reservaSala.getSegmentoInicial()));
        q.setParameter("segmentoTermino",
                formatoHora2.format(reservaSala.getSegmentoFinal()));
        List<ReservaSala> listaReservas = q.getResultList();
        return listaReservas;
    }

    /** {@inheritDoc} */
    public ReservaSala buscarReservaSala(ReservaSala datoSala) {
        ReservaSala reservaSala;
        reservaSala = null;
        String sql =
                "SELECT rs from ReservaSala rs"
                        + " where cod_reserva=:codReserva";
        Query q = em.createQuery(sql);
        q.setParameter("codReserva", datoSala.getCodReserva());
        reservaSala = (ReservaSala) q.getSingleResult();
        return reservaSala;
    }

    /** {@inheritDoc} */
    public List<ReservaSala> buscarReservaAntesAgregar(int codSala,
            String fechaSeleccionada) {
        Query q =
                em.createQuery("SELECT rs from ReservaSala rs where "
                        + "ADDTIME(segmento_inicial,'00:00:01') "
                        + "BETWEEN=:segmentoInicio and=:segmentoTermino");
        q.setParameter("codSala", codSala);
        q.setParameter("fechaInicio", fechaSeleccionada);
        // query.setParameter("login", usuario.getLogin());
        List<ReservaSala> listaReservas = q.getResultList();
        return listaReservas;
    }

    /** {@inheritDoc} */
    public List<ReservaSala> buscarListaReservaSalaNormal(int codSala,
            String fechaSeleccionada, String username) {
        Query q =
                em.createQuery("SELECT rs from ReservaSala rs "
                        + "where cod_sala_reserva=:codSala "
                        + "and fecha_inicio=:fechaInicio and estado=1 "
                        + "and username_reserva=:username");
        q.setParameter("codSala", codSala);
        q.setParameter("fechaInicio", fechaSeleccionada);
        q.setParameter("username", username);
        // query.setParameter("login", usuario.getLogin());
        List<ReservaSala> listaReservas = q.getResultList();
        return listaReservas;
    }

    /** {@inheritDoc} */
    public List<Segmentos> obtenerSegmentos() {
        Query q = em.createQuery("SELECT s from Segmentos s");
        List<Segmentos> segmentos = q.getResultList();
        return segmentos;
    }

    /**
     * Allows to get the em attribute value.
     * 
     * @return the em value.
     */
    public EntityManager getEm() {
        return em;
    }

    /**
     * Allows to set the em attribute value.
     * 
     * @param em
     *            new value for em attribute.
     */
    public void setEm(EntityManager em) {
        this.em = em;
    }

}
